﻿Imports SdlDotNet.Graphics
Imports SdlDotNet.Graphics.Sprites
Imports SdlDotNet.Input
Imports System.Drawing

Public Class DeployWeaponMenu
    Implements IMenu

    Private Position As Point

    Private SmallMissileAvailableSurface As New Surface("Resources\Images\Game\Menu\SmallMissile_Available.jpg")
    Private SmallMissileDeployedSurface As New Surface("Resources\Images\Game\Menu\SmallMissileDeployed_unAvailable.jpg")
    Private SmallMissileUnavailableSurface As New Surface("Resources\Images\Game\Menu\SmallMissile_Unavailable.jpg")
    Private SmallMissileSelectedSurface As New Surface("Resources\Images\Game\Menu\SmallMissile_Selected.jpg")
    Private MediumMissileAvailableSurface As New Surface("Resources\Images\Game\Menu\MediumMissile_Available.jpg")
    Private MediumMissileDeployedSurface As New Surface("Resources\Images\Game\Menu\MediumMissileDeployed_unAvailable.jpg")
    Private MediumMissileUnavailableSurface As New Surface("Resources\Images\Game\Menu\MediumMissile_Unavailable.jpg")
    Private MediumMissileSelectedSurface As New Surface("Resources\Images\Game\Menu\MediumMissile_Selected.jpg")
    Private LargeMissileAvailableSurface As New Surface("Resources\Images\Game\Menu\LargeMissile_Available.jpg")
    Private LargeMissileDeployedSurface As New Surface("Resources\Images\Game\Menu\LargeMissileDeployed_Unavailable.jpg")
    Private LargeMissileUnavailableSurface As New Surface("Resources\Images\Game\Menu\LargeMissile_Unavailable.jpg")
    Private LargeMissileSelectedSurface As New Surface("Resources\Images\Game\Menu\LargeMissile_Selected.jpg")
    Private ExtraLargeMissileAvailableSurface As New Surface("Resources\Images\Game\Menu\ExtraLargeMissile_Available.jpg")
    Private ExtraLargeMissileDeployedSurface As New Surface("Resources\Images\Game\Menu\ExtraLargeMissileDeployed_Unavailable.jpg")
    Private ExtraLargeMissileUnavailableSurface As New Surface("Resources\Images\Game\Menu\ExtraLargeMissile_Unavailable.jpg")
    Private ExtraLargeMissileSelectedSurface As New Surface("Resources\Images\Game\Menu\ExtraLargeMissile_Selected.jpg")
    Private BomberAvailableSurface As New Surface("Resources\Images\Game\Menu\Bomber_Available.jpg")
    Private BomberUnavailableSurface As New Surface("Resources\Images\Game\Menu\Bomber_Unavailable.jpg")
    Private BomberSelectedSurface As New Surface("Resources\Images\Game\Menu\Bomber_Selected.jpg")
    Private BomberDeployedSurface As New Surface("Resources\Images\Game\Menu\BomberDeployed_Unavailable.jpg")
    Private CancelAvailableSurface As New Surface("Resources\Images\Game\Menu\Cancel_Available.jpg")
    Private CancelSelectedSurface As New Surface("Resources\Images\Game\Menu\Cancel_Selected.jpg")


    Private SmallMissileButton As Sprite
    Private MediumMissileButton As Sprite
    Private LargeMissileButton As Sprite
    Private ExtraLargeMissileButton As Sprite
    Private BomberButton As Sprite
    Private CancelButton As Sprite

    Private MissileDeployed As MissileAndWarheadTypes = MissileAndWarheadTypes.None
    Private BomberDeployed As Boolean = False

    Private CanSelectSmallMissile As Boolean = False
    Private CanSelectMediumMissile As Boolean = False
    Private CanSelectLargeMissile As Boolean = False
    Private CanSelectExtraLargeMissile As Boolean = False
    Private CanSelectBomber As Boolean = False

    Public Event ClickedMissileButton(ByVal missileType As MissileAndWarheadTypes)
    Public Event ClickedBomberButton()
    Public Event ClickedCancelButton()

    Public Sub New(ByVal position As Point, ByVal player As Player)
        Me.Position = position
        InitializeSprites()
        ShowMenuForPlayer(player)
    End Sub

    Private Sub InitializeSprites()
        SmallMissileButton = New Sprite(SmallMissileUnavailableSurface, New Point(Position.X, Position.Y))
        MediumMissileButton = New Sprite(MediumMissileUnavailableSurface, New Point(Position.X, Position.Y + 19))
        LargeMissileButton = New Sprite(LargeMissileUnavailableSurface, New Point(Position.X, Position.Y + (19 * 2)))
        ExtraLargeMissileButton = New Sprite(ExtraLargeMissileUnavailableSurface, New Point(Position.X, Position.Y + (19 * 3)))
        BomberButton = New Sprite(BomberUnavailableSurface, New Point(Position.X, Position.Y + (19 * 4)))
        CancelButton = New Sprite(CancelAvailableSurface, New Point(Position.X, Position.Y + (19 * 5)))
    End Sub

    Public Sub Draw(ByRef display As Surface) Implements IMenu.Draw
        display.Blit(SmallMissileButton)
        display.Blit(MediumMissileButton)
        display.Blit(LargeMissileButton)
        display.Blit(ExtraLargeMissileButton)
        display.Blit(BomberButton)
        display.Blit(CancelButton)
    End Sub

    Public Sub HandleMouseButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs) Implements IMenu.HandleMouseButtonUp
        If (SmallMissileButton.IntersectsWith(e.Position) And CanSelectSmallMissile) Then
            RaiseEvent ClickedMissileButton(MissileAndWarheadTypes.Small)
        ElseIf (MediumMissileButton.IntersectsWith(e.Position) And CanSelectMediumMissile) Then
            RaiseEvent ClickedMissileButton(MissileAndWarheadTypes.Medium)
        ElseIf (LargeMissileButton.IntersectsWith(e.Position) And CanSelectLargeMissile) Then
            RaiseEvent ClickedMissileButton(MissileAndWarheadTypes.Large)
        ElseIf (ExtraLargeMissileButton.IntersectsWith(e.Position) And CanSelectExtraLargeMissile) Then
            RaiseEvent ClickedMissileButton(MissileAndWarheadTypes.ExtraLarge)
        ElseIf (BomberButton.IntersectsWith(e.Position) And CanSelectBomber) Then
            RaiseEvent ClickedBomberButton()
        ElseIf (CancelButton.IntersectsWith(e.Position)) Then
            RaiseEvent ClickedCancelButton()
        End If
    End Sub

    Public Sub HandleMouseMotion(ByVal sender As Object, ByVal e As MouseMotionEventArgs) Implements IMenu.HandleMouseMotion
        ApplyPlayerView()

        If (SmallMissileButton.IntersectsWith(e.Position) And CanSelectSmallMissile) Then
            SmallMissileButton.Surface = SmallMissileSelectedSurface
        ElseIf (MediumMissileButton.IntersectsWith(e.Position) And CanSelectMediumMissile) Then
            MediumMissileButton.Surface = MediumMissileSelectedSurface
        ElseIf (LargeMissileButton.IntersectsWith(e.Position) And CanSelectLargeMissile) Then
            LargeMissileButton.Surface = LargeMissileSelectedSurface
        ElseIf (ExtraLargeMissileButton.IntersectsWith(e.Position) And CanSelectExtraLargeMissile) Then
            ExtraLargeMissileButton.Surface = ExtraLargeMissileSelectedSurface
        ElseIf (BomberButton.IntersectsWith(e.Position) And CanSelectBomber) Then
            BomberButton.Surface = BomberSelectedSurface
        ElseIf (CancelButton.IntersectsWith(e.Position)) Then
            CancelButton.Surface = CancelSelectedSurface
        End If
    End Sub

    Public Sub ShowMenuForPlayer(ByRef player As Player) Implements IMenu.ShowMenuForPlayer
        If (player.Inventory.SmallMissiles > 0 And player.MissileDeployed <> MissileAndWarheadTypes.Small) Then
            CanSelectSmallMissile = True
        Else
            CanSelectSmallMissile = False
        End If
        If (player.Inventory.MediumMissiles > 0 And player.MissileDeployed <> MissileAndWarheadTypes.Medium) Then
            CanSelectMediumMissile = True
        Else
            CanSelectMediumMissile = False
        End If
        If (player.Inventory.LargeMissiles > 0 And player.MissileDeployed <> MissileAndWarheadTypes.Large) Then
            CanSelectLargeMissile = True
        Else
            CanSelectLargeMissile = False
        End If
        If (player.Inventory.ExtraLargeMissiles > 0 And player.MissileDeployed <> MissileAndWarheadTypes.ExtraLarge) Then
            CanSelectExtraLargeMissile = True
        Else
            CanSelectExtraLargeMissile = False
        End If
        If (player.Inventory.Bombers > 0 And player.BomberDeployed = False) Then
            CanSelectBomber = True
        Else
            CanSelectBomber = False
        End If

        MissileDeployed = player.MissileDeployed
        BomberDeployed = player.BomberDeployed

        HandleMouseMotion(Me, New MouseMotionEventArgs(False, MouseButton.None, CShort(Mouse.MousePosition.X), CShort(Mouse.MousePosition.Y), CShort(Mouse.MousePositionChange.X), CShort(Mouse.MousePositionChange.Y)))
    End Sub

    Private Sub ApplyPlayerView()
        CancelButton.Surface = CancelAvailableSurface

        If (CanSelectSmallMissile) Then
            SmallMissileButton.Surface = SmallMissileAvailableSurface
        Else
            SmallMissileButton.Surface = SmallMissileUnavailableSurface
        End If

        If (CanSelectMediumMissile) Then
            MediumMissileButton.Surface = MediumMissileAvailableSurface
        Else
            MediumMissileButton.Surface = MediumMissileUnavailableSurface
        End If

        If (CanSelectLargeMissile) Then
            LargeMissileButton.Surface = LargeMissileAvailableSurface
        Else
            LargeMissileButton.Surface = LargeMissileUnavailableSurface
        End If

        If (CanSelectExtraLargeMissile) Then
            ExtraLargeMissileButton.Surface = ExtraLargeMissileAvailableSurface
        Else
            ExtraLargeMissileButton.Surface = ExtraLargeMissileUnavailableSurface
        End If

        If (CanSelectBomber) Then
            BomberButton.Surface = BomberAvailableSurface
        Else
            BomberButton.Surface = BomberUnavailableSurface
        End If

        If (BomberDeployed) Then
            BomberButton.Surface = BomberDeployedSurface
        End If
        If (MissileDeployed = MissileAndWarheadTypes.Small) Then
            SmallMissileButton.Surface = SmallMissileDeployedSurface
        ElseIf (MissileDeployed = MissileAndWarheadTypes.Medium) Then
            MediumMissileButton.Surface = MediumMissileDeployedSurface
        ElseIf (MissileDeployed = MissileAndWarheadTypes.Large) Then
            LargeMissileButton.Surface = LargeMissileDeployedSurface
        ElseIf (MissileDeployed = MissileAndWarheadTypes.ExtraLarge) Then
            ExtraLargeMissileButton.Surface = ExtraLargeMissileDeployedSurface
        End If
    End Sub

    Public Sub Dispose() Implements IMenu.Dispose
        SmallMissileAvailableSurface.Dispose()
        SmallMissileUnavailableSurface.Dispose()
        SmallMissileSelectedSurface.Dispose()
        MediumMissileAvailableSurface.Dispose()
        MediumMissileUnavailableSurface.Dispose()
        MediumMissileSelectedSurface.Dispose()
        LargeMissileAvailableSurface.Dispose()
        LargeMissileUnavailableSurface.Dispose()
        LargeMissileSelectedSurface.Dispose()
        ExtraLargeMissileAvailableSurface.Dispose()
        ExtraLargeMissileUnavailableSurface.Dispose()
        ExtraLargeMissileSelectedSurface.Dispose()
        CancelAvailableSurface.Dispose()
        CancelSelectedSurface.Dispose()
        BomberAvailableSurface.Dispose()
        BomberUnavailableSurface.Dispose()
        BomberSelectedSurface.Dispose()
        SmallMissileDeployedSurface.Dispose()
        MediumMissileDeployedSurface.Dispose()
        LargeMissileDeployedSurface.Dispose()
        ExtraLargeMissileDeployedSurface.Dispose()
    End Sub
End Class